package com.pi.pro.xcmg.am.was.web.wcs.controller;


import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.pi.common.utils.LogSupport;
import com.pi.common.utils.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.edi.wcs.common.WcsWasExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.wcs.dto.*;
import com.pi.pro.xcmg.am.was.fac.service.LedCustomWorkService;
import com.pi.pro.xcmg.am.was.fac.service.WcsCustomWorkApiService;
import com.pi.wms.dto.act.TaskActionDto;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;


/**
 * 任务变更
 */
@Slf4j
@RestController
@RequestMapping("/WebApi/api/JK/")
public class WcsWorkApiController {

    protected Logger ioLogger= LogSupport.getIoLogger();

    @Autowired
    private WcsCustomWorkApiService commonWorkService;

    @Autowired
    private LedCustomWorkService ledCustomWorkService;


    private Cache<String, String> containNumCache= CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build();

    @PostMapping(value="/ScanBarcodeInbound")
    public CommonResultData moveRequest(@RequestBody MoveRequestData requestData){
        //
        log.debug("ApiController WcsTaskApiController ScanBarcodeInbound://{}",requestData);
        ioLogger.debug(">>>> [MOVE-REQ] //{}",requestData);
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(XcmgAmConstants.DIVISION_WH101_CODE);
        String m = this.commonWorkService.moveRequest(Arrays.asList(requestData), actionDto);
        if (!StringUtils.isNullOrBlank(m)){
            ioLogger.debug(">>>> [MOVE-REQ] stop//{}",m);
            if (m.startsWith(XcmgAmConstants.RESULT_CODE_EDI_WARN_GONO)){
                return WcsWasExchangeSupport.ok();
            }
            if (m.startsWith(XcmgAmConstants.RESULT_CODE_EDI_WARN_FEEDBACK)){
                return WcsWasExchangeSupport.feedback(m);
            }
            return WcsWasExchangeSupport.error(m);
        }
        return WcsWasExchangeSupport.ok();
    }

    @PostMapping(value="/ReceiveLedMessage")
    public CommonResultData checkStatus(@RequestBody InboundCheckData checkData){
        log.debug("ApiController WcsTaskApiController ReceiveLedMessage://{}", checkData);
        ioLogger.debug(">>>> [CHECK-STATUS] //{}", checkData);
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(XcmgAmConstants.DIVISION_WH101_CODE);
        String m = this.commonWorkService.checkStatus(checkData, actionDto);
        if (!StringUtils.isNullOrBlank(m)){
            return WcsWasExchangeSupport.error(m);
        }
        return WcsWasExchangeSupport.ok();
    }

    @PostMapping(value="/ReceiveTaskResult")
    public CommonResultData taskReport(@RequestBody TaskReportData reportData){
        log.debug("ApiController WcsTaskApiController ReceiveTaskResult://{}",reportData);
        ioLogger.debug(">>>> [TASK-CHANGE] //{}",reportData);
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(XcmgAmConstants.DIVISION_WH101_CODE);
        String m = this.commonWorkService.taskChange(reportData, actionDto);
        if (!StringUtils.isNullOrBlank(m)){
            ioLogger.debug(">>>> [TASK-CHANGE] stop//{}",m);
            if (m.startsWith(XcmgAmConstants.RESULT_CODE_EDI_WARN_GONO)){
                return WcsWasExchangeSupport.ok();
            }
            /**
            if (m.startsWith(XcmgAmConstants.RESULT_CODE_EDI_WARN_RETRUN)){
                return WcsWasExchangeSupport.checked();
            }
             **/
            return WcsWasExchangeSupport.error(m);
        }
        return WcsWasExchangeSupport.ok();
    }

    @PostMapping(value="/ReceiveSiteStatus")
    public CommonResultData stationReport(@RequestBody StationReportData reportData){
        log.debug("ApiController WcsTaskApiController ReceiveSiteStatus://{}",reportData);
        ioLogger.debug(">>>> [STATION-CHANGE] //{}",reportData);
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(XcmgAmConstants.DIVISION_WH101_CODE);
        String m = this.commonWorkService.deviceChange(reportData, actionDto);
        if (!StringUtils.isNullOrBlank(m)){
            return WcsWasExchangeSupport.error(m);
        }
        return WcsWasExchangeSupport.ok();
    }

    @PostMapping(value="/ReceiveCallPallet")
    public CommonResultData replenishEmptyPallet(@RequestBody EPalletRequestData requestData){
        log.debug("ApiController WcsTaskApiController ReceiveCallPallet://{}",requestData);
        ioLogger.debug(">>>> [EPAL-REPL] //{}",requestData);
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(XcmgAmConstants.DIVISION_WH101_CODE);
        String m = this.commonWorkService.epalReplenish(requestData, actionDto);
        if (!StringUtils.isNullOrBlank(m)){
            return WcsWasExchangeSupport.error(m);
        }
        return WcsWasExchangeSupport.ok();
    }

    @PostMapping(value="/cleanledcache")
    public CommonResultData cleanLedCache(){
        log.debug("ApiController WcsTaskApiController cleanCache://{}");
        ioLogger.debug(">>>> [CLEAN-LED-CACHE] //");
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(XcmgAmConstants.DIVISION_WH101_CODE);
        String m = this.ledCustomWorkService.cleanCache();
        if (!StringUtils.isNullOrBlank(m)){
            return WcsWasExchangeSupport.error(m);
        }
        return WcsWasExchangeSupport.ok();
    }

}
